<template>
    <div>
        <global-tables
            @handleAllSelect="handleAllSelect"
            @changePage="handleChangePage"
            :list="list"
            :total="total"
            :loading="loading"
            :columns="columns"
            :current="search.Page"
        >
            <Form :model="search" :label-width="80">
                <div class="table-top-wrap">
                    <div>
                        <div>
                            <Button v-if="isAddBtn" class="features-btn" @click="handleBtnClick(null,'add')">
                                <Icon type="md-add" size="18" />新建
                            </Button>
                            <!-- <Button v-if="isDelete" class="features-btn btn-auto-m" @click="handleBtnClick(null,'delete')">
                                <Icon type="md-trash" size="17" />删除
                            </Button>-->
                            <!-- <Button v-if="isProhibit" class="features-btn btn-auto-m" @click="handleBtnClick(null,'on')">
                                <Icon type="ios-unlock-outline" size="17" />启用
                            </Button>
                            <Button v-if="isProhibit" class="features-btn btn-auto-m" @click="handleBtnClick(null,'off')">
                                <Icon type="ios-lock-outline" size="17" />停用
                            </Button>-->
                        </div>
                    </div>
                    <div class="table-top-wrap-bottom">
                        <div>
                            <Select v-model="search.type" style="width:60px;margin-right:10px" placeholder="类别">
                                <Option v-for="item in categoryList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                            </Select>
                        </div>
                        <div>
                            <input type="text" class="style-input" @keyup.enter="handleSearch" v-model="search.keyword" placeholder="请输入服务名称" />
                            <i-input style="display:none"></i-input>
                        </div>
                        <div>
                            <Button icon="ios-search" @click="handleSearch" class="search-btn">搜索</Button>
                        </div>
                    </div>
                </div>
            </Form>
        </global-tables>
        <Modal :mask-closable="false" v-model="isModal" :title="ModalTitle" width="800">
            <div v-if="isModal">
                <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="80">
                    <Row>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="服务名称" prop="ServiceName">
                                <i-input v-model="formItem.ServiceName" placeholder="请输入服务名称"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 8}">
                            <FormItem label="服务类别" prop="ServiceType">
                                <Select v-model="formItem.ServiceType">
                                    <Option v-for="item in ServerList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                                </Select>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 8}">
                            <FormItem label="服务价格" prop="ServicePrice">
                                <i-input type="number" v-model="formItem.ServicePrice" placeholder="请输入价格"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 8}">
                            <FormItem label="护士资格" prop="ServiceLevel">
                                <Select v-model="formItem.ServiceLevel">
                                    <Option v-for="item in ServiceLevelList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                                </Select>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="服务图片 750 * 400">
                                <img-upload
                                    v-if="isImg"
                                    :defaultList="imgDefaultList.Picture"
                                    @delImgList="delImgList"
                                    :multiple="false"
                                    :data="{directory:'NewsPath'}"
                                    :check="1"
                                    :value="'Picture'"
                                    @setImgLIst="setImgLIst"
                                ></img-upload>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="服务描述" prop="Description">
                                <i-input type="textarea" v-model="formItem.Description" placeholder="请输入简述"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="详细说明">
                                <editor ref="editor" :value="content" @on-change="handleChange" />
                            </FormItem>
                        </i-col>
                    </Row>
                </Form>
            </div>
            <div slot="footer">
                <Button size="large" @click="handleClose">取消</Button>
                <Button type="primary" size="large" @click="handleSubmit('formItem')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
/**
 * 品牌列表
 */
import { cyclicRouting, getdate } from '@/util';
import { getModulePermission } from '@/api/user';
import GlobalTables from '@/components/GlobalTables';
import Editor from '@/components/Editor';
import ImgUpload from '@/components/ImgUpload';
import Api from './service';

export default {
    name: 'ProductsBrand',
    components: {
        GlobalTables,
        Editor,
        ImgUpload
    },
    data() {
        return {
            /**
             *  list 表格数据
             *  itemId 对话框id : 修改的时候需要
             *  allSelectArr 多选数据 : 批量删除或者停用
             *  loading 表格加载
             *  total 表格总页
             *  formItem 表单数据 : 提交表单对应的数据
             *  ruleValidate 表单校验
             *  isDelete 删除按钮
             *  isAddBtn 添加按钮
             *  isProhibit 停用按钮
             *  isModal 对话框状态
             *  ModalTitle  对话框标题
             *  search  请求数据 : 分页 模糊搜索
             *  columns 表头
             *  validatorSupplierID 自定义校验
             */
            //
            Description: '',
            content: '',
            isImg: true,
            imglist: {
                Picture: []
            },
            imgDefaultList: {
                Picture: []
            },
            list: [],
            total: 0,
            itemId: 0,
            loading: true,
            isDelete: false,
            isAddBtn: false,
            isProhibit: false,
            isModal: false,
            allSelectArr: null,
            ModalTitle: '',
            formItem: this.initFormItem(),
            search: {
                keyword: '',
                pageindex: 1,
                pagesize: 10,
                type: '0'
            },
            categoryList: [
                {
                    label: '全部',
                    value: '0'
                },
                {
                    label: '陪诊',
                    value: '1'
                },
                {
                    label: '护理',
                    value: '2'
                }
            ],
            ServerList: [
                {
                    label: '陪诊',
                    value: '1'
                },
                {
                    label: '护理',
                    value: '2'
                }
            ],
            ServiceLevelList: [
                {
                    label: '护士',
                    value: '1'
                },
                {
                    label: '护师',
                    value: '2'
                },
                {
                    label: '主管护师',
                    value: '3'
                }
            ],
            ruleValidate: {
                ServiceName: [
                    {
                        required: true,
                        message: '请输入服务名称',
                        trigger: 'change'
                    }
                ],
                ServiceType: [
                    {
                        required: true,
                        message: '请选择服务类别',
                        trigger: 'change'
                    }
                ],
                ServiceLevel: [
                    {
                        required: true,
                        message: '请选择护士资格',
                        trigger: 'change'
                    }
                ],
                ServicePrice: [
                    {
                        required: true,
                        message: '请输入价格',
                        trigger: 'change'
                    }
                ]
            },
            /* 表头 */
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '服务名称',
                    key: 'ServiceName'
                },
                {
                    title: '服务类别',
                    key: 'ServiceType',
                    width: 150,
                    align: 'center'
                },
                {
                    title: '护士资格',
                    key: 'ServiceLevelName',
                    width: 100,
                    align: 'center'
                },
                {
                    title: '服务价格',
                    key: 'ServicePrice',
                    width: 100,
                    align: 'center'
                },
                {
                    title: '图片',
                    width: 100,
                    align: 'center',
                    render: (h, params) => {
                        const { Picture } = params.row;
                        return h('div', [
                            h('Avatar', {
                                props: {
                                    src: Picture,
                                    shape: 'square',
                                    size: 'large'
                                },
                                style: {
                                    margin: '5px 0'
                                }
                            })
                        ]);
                    }
                },
                // {
                //     title: '状态',
                //     width: 80,
                //     render: (h, params) => {
                //         const { IsActivity } = params.row;
                //         if (IsActivity) {
                //             return h('div', [
                //                 h('Badge', {
                //                     props: {
                //                         status: 'processing',
                //                         text: '正常'
                //                     }
                //                 })
                //             ]);
                //         } else {
                //             return h('div', [
                //                 h('Badge', {
                //                     props: {
                //                         status: 'error',
                //                         text: '停用'
                //                     }
                //                 })
                //             ]);
                //         }
                //     }
                // },
                {
                    title: '状态',
                    width: 100,
                    align: 'center',
                    render: (h, params) => {
                        return h(
                            'i-switch',
                            {
                                props: {
                                    type: 'primary',
                                    value: params.row.IsActivity //控制开关的打开或关闭状态，官网文档属性是value
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    'on-change': () => {
                                        Api.setDisableEnable({
                                            id: params.row.ID
                                        }).then(res => {
                                            if (res.state == 'success') {
                                                this.$Message.success('操作成功');
                                                this.getTablesListData();
                                            } else {
                                                this.$Message.error(res.message);
                                            }
                                        });
                                    }
                                }
                            },
                            [
                                h('span', {
                                    slot: 'open',
                                    domProps: {
                                        innerHTML: '是'
                                    }
                                }),
                                h('span', {
                                    slot: 'close',
                                    domProps: {
                                        innerHTML: '否'
                                    }
                                })
                            ]
                        );
                    }
                },
                {
                    title: '时间',
                    width: 120,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            params.row.CreateTime &&
                                h(
                                    'span',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        }
                                    },
                                    getdate(new Date(params.row.CreateTime).getTime())
                                )
                        ]);
                    }
                },
                {
                    title: '操作',
                    width: 80,
                    align: 'center',
                    render: (h, params) => {
                        // params.row.IsActivity &&
                        const { isEdit } = this;
                        return h('div', [
                            isEdit &&
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            background: '#5ab1ef',
                                            border: 'none',
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.handleBtnClick(params.row, 'edit');
                                            }
                                        }
                                    },
                                    '修改'
                                )
                        ]);
                    }
                }
            ]
        };
    },
    methods: {
        handleChange(html) {
            this.formItem.Detail = html;
        },
        setImgLIst(list, value) {
            this.imglist[value] = list;
        },
        /* 图片删除 */
        delImgList(list, value) {
            this.imglist[value] = list;
        },
        /* 关闭对话框 */
        handleClose() {
            this.isModal = false;
        },
        /* 提交 */
        handleSubmit(name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    if (this.imglist.Picture && this.imglist.Picture.length > 0) {
                        this.formItem.Picture = this.imglist.Picture[0].url;
                    }
                    let data = this.formItem;
                    Api.addTablesItem(data).then(res => {
                        if (res.state == 'success') {
                            this.$Message.success('操作成功');
                            this.loading = true;
                            this.isModal = false;
                            this.getTablesListData();
                        } else {
                            this.$Message.error(res.message);
                        }
                    });
                }
            });
        },
        /* 供应商选择 */
        setDictionaries(item, value) {
            this.formItem[value] = item.ID + ' ';
        },
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            Api.getTablesList(data).then(res => {
                this.list = res.data.rows;
                this.loading = false;
                this.total = res.data.records;
            });
        },
        /* 表格查询 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 对话框 */
        handleBtnClick(item, type) {
            this.ModalTitle = '新建服务';
            this.formItem = this.initFormItem();
            if (type == 'edit' || type == 'add') {
                this.imgDefaultList.Picture = [];
                this.imglist.Picture = [];
                this.content = ' ';
                this.formItem.Description = '';
                this.itemId = 0;
                if (type == 'edit') {
                    this.itemId = item.ID;
                    this.ModalTitle = '修改服务';
                    this.isImg = false;
                    this.imgDefaultList.Picture = [];
                    Api.getItemInfo({
                        id: item.ID
                    }).then(res => {
                        this.isModal = true;
                        this.formItem = {
                            ...res.data,
                            ServiceType: res.data.ServiceType + '',
                            ServicePrice: res.data.ServicePrice + ''
                        };
                        this.imgDefaultList.Picture.push({
                            url: res.data.Picture
                        });
                        this.imglist.Picture.push({
                            url: res.data.Picture
                        });
                        this.isImg = true;
                        this.content = res.data.Detail;
                        // this.$refs.editor.setHtml(res.data.Description);
                    });
                } else {
                    this.isModal = true;
                }
            } else if (type === 'off' || type === 'on') {
                let title = '';
                if (type == 'off') {
                    title = '停用';
                } else {
                    title = '启用';
                }
                if (this.allSelectArr) {
                    this.$Modal.confirm({
                        title: `你确定要${title}吗`,
                        onOk: () => {
                            Api.setDisableEnable({
                                id: this.allSelectArr
                                // value: type == 'off' ? 0 : 1
                            }).then(res => {
                                if (res.state == 'success') {
                                    this.$Message.success(res.message);
                                    this.allSelectArr = '';
                                } else {
                                    this.$Message.error(res.message);
                                }
                                this.getTablesListData();
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('取消成功');
                        }
                    });
                } else {
                    this.$Message.error('请先选择需要操作的记录');
                }
            } else if (type === 'delete') {
                if (this.allSelectArr) {
                    this.$Modal.confirm({
                        title: `你确定要删除吗`,
                        onOk: () => {
                            Api.deleteItem({
                                keyValue: this.allSelectArr
                            }).then(res => {
                                if (res.state == 'success') {
                                    this.$Message.success(res.message);
                                    this.allSelectArr = '';
                                } else {
                                    this.$Message.error(res.message);
                                }
                                this.getTablesListData();
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('取消成功');
                        }
                    });
                } else {
                    this.$Message.error('请先选择需要操作的记录');
                }
            }
        },
        /* 表单数据 */
        initFormItem() {
            return {
                ID: 0
            };
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesizeize = page;
                this.getTablesListData();
            }
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        }
    },
    mounted() {
        /* 获取菜单的id */
        cyclicRouting(JSON.parse(sessionStorage.getItem('user')).menuList, this.$route, id => {
            /* 按钮权限请求 */
            getModulePermission({
                roleid: JSON.parse(sessionStorage.getItem('user')).roleid,
                moduleid: id
            }).then(res => {
                /* 按钮权限赋值 */
                for (let item in res.data) {
                    if (res.data[item].name == 'add') {
                        this.isAddBtn = true;
                    } else if (res.data[item].name == 'delete') {
                        this.isDelete = true;
                    } else if (res.data[item].name == 'edit') {
                        this.isEdit = true;
                    } else if (res.data[item].name == 'onoff') {
                        this.isProhibit = true;
                    }
                }
                /* 列表请求 */
                this.getTablesListData();
            });
        });
    }
};
</script>

<style>
</style>
